package com.sk.skplanet.commute.actions;

import java.io.IOException;
import java.io.StringReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;

import com.sk.skplanet.commute.R;
import com.sk.skplanet.commute.controllers.SeoulBusApiController;
import com.sk.skplanet.commute.vo.API_RequestInfo;

public class API_requestBus extends AsyncTask<String, Integer, String> {
	private String arsId;
	private Context context;
	private API_RequestInfo api_requestinfo;
	private TextView textview_arriveTime;
//	private TextView textview_busNum;
	private TextView textview_startTime;
	private TextView textview_lastTime;
	private TextView textview_arrivalstop;

	public API_requestBus(Context context, String arsId, API_RequestInfo api_requestinfo, TextView[] textviewArr) {
		super();
		this.arsId = arsId;
		this.context = context;
		this.api_requestinfo = api_requestinfo;
		this.textview_arrivalstop = textviewArr[4];
		this.textview_lastTime = textviewArr[3];
		this.textview_startTime = textviewArr[2];
		this.textview_arriveTime = textviewArr[1];
//		this.textview_busNum = textviewArr[0];
	}

	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);

		try {
			XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
			XmlPullParser parser = parserCreator.newPullParser();

			parser.setInput(new StringReader(result));
			parser.next();

			int parserEvent = parser.getEventType();

			String tagName = "";
			boolean isAgreement = false;

			while (parserEvent != XmlPullParser.END_DOCUMENT) {

				switch (parserEvent) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					tagName = parser.getName();
					break;
				case XmlPullParser.END_TAG:
					if (isAgreement) {
						if (parser.getName().equals("itemList")) {
							isAgreement = false;
						}
					}
					break;
				case XmlPullParser.TEXT:
					// if("busRouteNm".equals(tagName)){
					// Log.i("parser","번호 : "+parser.getText());
					// }
					// if("busRouteType".equals(tagName)){
					// //(1:공항, 3:간선, 4:지선, 5:순환, 6:광역, 7:인천, 8:경기, 9:폐지, 0:공용)
					// Log.i("parser","버스종류 : "+parser.getText());
					// }
					if ("arsId".equals(tagName)) {
						if (arsId.equals(parser.getText())) {
							isAgreement = true;
						}
					}

					if (isAgreement) {
						if ("kals1".equals(tagName)) { // 첫번째 오는 버스의 도착 예정시간
							if (api_requestinfo != null) api_requestinfo.setArrivalTime(parser.getText());
							// Log.i("parser", "도착 예정 시간" + parser.getText());
						}
						if ("firstTm".equals(tagName)) { // 첫차 운행 시간
							if (api_requestinfo != null) api_requestinfo.setStartTime(Actions.setBusTime(parser
									.getText()));
							// Log.i("parser", "첫차시간" + parser.getText());
						}
						if ("lastTm".equals(tagName)) { // 막차 운행 시간
							if (api_requestinfo != null) api_requestinfo.setLastTime(" ~ "
									+ Actions.setBusTime(parser.getText()));
							// Log.i("parser", "막차시간" + parser.getText());
						}
						if ("term".equals(tagName)) { // 배차간격
							if (api_requestinfo != null) api_requestinfo.setIntervalTime(parser.getText());
							// Log.i("parser", "배차간격" + parser.getText());

						}
						if ("sectOrd1".equals(tagName)) { // 첫번째 도착 버스 순번
							// Log.i("parser", "첫번째도착 버스 순번" + parser.getText());
							api_requestinfo.setArrivalStop(parser.getText());
						}
						if ("staOrd".equals(tagName)) { // 요청 정류장 순번
							// Log.i("parser", "요청정류장순번" + parser.getText());
							int stop = Integer.parseInt(api_requestinfo.getArrivalStop());
							api_requestinfo.setArrivalStop(Integer.toString(Integer.parseInt(parser.getText()) - stop));
						}

						if ("routeType".equals(tagName)) { // 버스 타입
							if (api_requestinfo != null) api_requestinfo.setBusType(parser.getText());
							// Log.i("parser", "버스타입" + parser.getText());
						}
					}

					break;
				}

				parserEvent = parser.next();
			}
			textview_arriveTime.setTag(api_requestinfo.getArrivalTime());
			textview_arriveTime.setText(Actions.setTime(api_requestinfo.getArrivalTime()));
			textview_startTime.setText(api_requestinfo.getStartTime());
			textview_lastTime.setText(api_requestinfo.getLastTime());

			textview_arrivalstop.setText(api_requestinfo.getArrivalStop()
					+ context.getResources().getString(R.string.textview_arrivalstop));
			// textview_busNum.setTextColor(Color.parseColor(Actions.setBusTypeColor(api_requestinfo.getBusType())));

		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);
	}

	@Override
	protected String doInBackground(String... arg0) {
		String result = "결과가 없습니다.";
		SeoulBusApiController sbac = new SeoulBusApiController("");

		try {
			if ("getRouteByStation".equals(arg0[0])) {
				result = sbac.getRouteByStation(arg0[1]);
			} else if ("getArrInfoByRouteAll".equals(arg0[0])) {
				result = sbac.getArrInfoByRouteAll(arg0[1]);
			} else if ("getArrInfoByRoute".equals(arg0[0])) {
				result = sbac.getArrInfoByRoute(arg0[1], arg0[2], arg0[3]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;

	}
}
